package common.service.base.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import common.service.base.annotation.MobileResponseBody;
import common.service.base.common.RespVo;
import common.service.base.exception.MobileException;
import common.service.contants.MobileConfig;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ExceptionHandler
 * @Description 异常处理器
 * @Author lixin
 * @Date 2020/02/10 下午12:07
 * Version 0.0.1
 **/
@ControllerAdvice
@ResponseBody
@Slf4j
public class MobileExceptionHandler {
    /**
     * 所有异常处理器
     * @param request
     * @param exception
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value=Exception.class)
    @MobileResponseBody
    public RespVo allExceptionHandler(HttpServletRequest request, Exception exception)throws Exception{
        RespVo respVo = null;
        if( exception instanceof MobileException){
            MobileException mobileException = (MobileException)exception;
            String status = mobileException.getStatus();
            String message = mobileException.getMessage();
            respVo=new RespVo(status,message);
            log.error("MobileException:"+exception.getMessage(),exception);
        }else {
            respVo=new RespVo(MobileConfig.MOBILE_STATUS_SYSTEM_ERROR, MobileConfig.MOBILE_MSG_SYSTEM_ERROR);
            log.error("exception:"+exception.getMessage(),exception);
        }
        respVo.setData(null);
        return respVo;
    }
}
